import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

public class Server {
		public void start() throws IOException{
			ServerSocket serversocket = new ServerSocket(8000);		
			System.out.println("Server ok...");
			
			while(true){
			try{
				Socket socket = serversocket.accept();             //获得客户端的socket；
				chat(socket);	

			}catch(Exception e){
				e.printStackTrace();
			}	
			}		
		}


		public void chat(final Socket socket){
			new Thread(new Runnable(){
				public void run( ){
					InputStream input = null;
					BufferedReader br =null;

					try{
						ClientManager cm =new ClientManager();
						cm.addClient(socket);
						input = socket.getInputStream();
						br = new BufferedReader(new InputStreamReader(input,"utf-8"));
						String msg ;
						while(null != (msg=br.readLine( ))){
							System.out.println(msg);	
						}
					}catch(Exception e){
						e.printStackTrace();
					}
					finally{
						try{
							if(null != input){
							input.close();
							}
						
							if(null != br){
							br.close();
							}
						
							if(null != socket){
							socket.close();
							}
						}
					catch(IOException ioe){
							
						}
												
					}
		
				}

			}).start();

		}
			
}
